<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/7/2
 * Time: 13:51
 */
namespace app\admin\controller;
use \think\Request;
use think\Db;
class Suggest extends Base
{
    protected $res = array(
        'index'   => array('css' => '2,3,4', 'js' => 'list,My97DatePicker'),
    );

    /**
     * 首页数据
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index(){
        $keywords = input('keywords');
        $page = input('param.page');
        $map = [];
        if($keywords&&$keywords!=""){
            $map['feedback_id|feedback_content'] = ['like',"%" . $keywords . "%"];
        }
        $list = Db::table('up_feedback')
            ->field('feedback_id,feedback_user_id,feedback_content,feedback_image,feedback_status,feedback_reply,feedback_reply_time')
            ->order('feedback_time desc')
            ->where($map)
            ->paginate(15);
        if(!$page){
            $pages = 0;
        }else{
            $pages = $page-1;
        }
        $action = '/' . Request::instance()->controller() . '/' . Request::instance()->action();
        $this->assign ([
            'page'=>$page,
            'list'=>$list,
            'pages'=>$pages,
            'action'=>$action,
            'keywords'=>$keywords,
        ]);
        return $this->fetch();
    }


    /**
     * delIndex 删除
     * @return \think\response\Json
     */
    public function delIndex(){
        $ids = strtoarray(input('post.ids'));
        $where['feedback_id'] = array('In', $ids);
        $result = Db::table('up_feedback')->where($where)->delete();
        if ($result) {
            return $this->api_success('操作成功');
        } else {
            return $this->api_dbError();
        }
    }

    /***
     * @return mixed
     * 查看详情
     */
    public function detail(){
        $id = input('id');
        if(!$id){
            $this->api_dbError('参数传递错误');
        }
        $res = Db::table('up_feedback')
            ->alias('f')
            ->join('up_u_user u','f.feedback_user_id = u.user_id','left')
            ->field('f.feedback_id,f.feedback_user_id,f.feedback_content,f.feedback_image,f.feedback_reply,f.feedback_reply_time,u.user_username,u.user_image,u.user_phone')
            ->where('f.feedback_id',$id)
            ->find();
        $this->assign('res',$res);
        return $this->fetch('detail');
    }

    public function reply(){
        $id = input('get.id');
        if(!$id){
            $this->api_dbError('参数传递错误');
        }
        $user_id = Db::table('up_feedback')->where('feedback_id',$id)->value('feedback_user_id');
        $this->assign ('user_id',$user_id);
        $this->assign('id',$id);
        return $this->fetch('reply');
    }

    /**
     * 回复反馈
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function edit(){
        extract(input());
        if($content ==""){
            return $this->api_error('回复内容不能为空', '14001');
        }
        $time = date('Y-m-d H:i:s');
        $param = [
            'n_title'=>'反馈回复',
            'n_content'=>$content,
            'n_createtime'=>$time,
            'n_type'=>4,
            'n_user_id'=>$user_id,
            'n_send_type'=>2,
            'n_read_users'=>',0,',
            'n_clear_users'=>',0,'
        ];
        // 启动事务
        Db::startTrans();
        try{
            Db::table('up_notice')->insert($param);
            Db::table('up_feedback')->where('feedback_id',$id)->update(['feedback_reply'=>$content,'feedback_reply_time'=>$time,'feedback_status'=>2]);
            // 提交事务
            Db::commit();
            return $this->api_success("操作成功");
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return $this->api_dbError("操作失败");
        }
    }

}